#region Copyright
/// <copyright>
/// Copyright (c) 2008 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace Osh.Dvb
{
	/// <summary>
	/// The Teletext descriptor is used in the PSI PMT to identify streams which carry EBU Teletext data.
	/// See ETSI EN 300 468 section 6.2.42 Teletext descriptor
	/// </summary>
	public class TeletextDescriptor : Descriptor
	{
		private List<TeletextPageDescription> _pages;

		public TeletextDescriptor(DescriptorReader reader, int length)
			: base(DescriptorTags.TeletextDescriptor, length)
		{
			_pages = new List<TeletextPageDescription>();
			while(length > 0)
			{
				int languageCode = (int)reader.ReadUInt32BE();
				int pageTypeAndMagazine = languageCode & 0xFF;
				languageCode >>= 8;

				TeletextPageTypes pageType = (TeletextPageTypes)((pageTypeAndMagazine >> 3) & 0x1F);
				int magazine = pageTypeAndMagazine & 8;
				int pageNumber = reader.ReadByte();

				_pages.Add(new TeletextPageDescription((LanguageCode)languageCode, pageType, magazine, pageNumber));

				length -= 5;
			}
		}

		public List<TeletextPageDescription> Pages
		{
			get { return _pages; }
		}
	}
}
